#include <kern/debug.h>
#include <kern/monitor.h>

#define BACKTRACE_ARGS 4
#define MAX_BACKTRACE 3


void imhere()
{
	panic("im here !");
}

void panic(char* msg)
{
	int* ebp;
	int eip;
	int i;

	__asm __volatile("cli");

	if (msg) {
		monitor_write(msg);
		monitor_write("\n");
	}
	
	for (ebp = (int*)(&msg) - 2, i = 0; ebp && i < MAX_BACKTRACE; ebp = (int*)(*ebp), ++i) {
		eip = ebp[1];
		monitor_write(" return to ");
		monitor_write_hex((int)eip);
		monitor_write(" args:");

		for (i=2; i < BACKTRACE_ARGS+2; ++i) {
			monitor_write("  ");
			monitor_write_hex(ebp[i]);
		} 
		monitor_write("\n");
	}
	
	while (1) {
		__asm __volatile("hlt");
	}
}

